﻿/// <reference path="../../../../Sustainalytics.EsgRatings.UI/modules/EsgRatings/utility/EsgUtility.ts" />
module Sustainalytics.Governance {
    import EsgUtility = Sustainalytics.EsgRatings.EsgUtility;

    export class GovernanceIndicatorsDetailsController {
        private govProfile: GovernanceProfileController;
        public currentCompanyId: string;
        private templates: KeyCGIssueTemplateDto[];
        private selectedIssue: IndicatorDataDto;
        private templateSelected: KeyCGIssueTemplateDto;
        private issueIndicatorsData: IndicatorDataIssue[];
        private selectCgOverallIssues: CompanyGovernanceIssueDto;

        constructor(
            public $scope: Company.ICompanyBasedScope,
            public $rootScope: ng.IScope,
            public $q: ng.IQService,
            private $location:ng.ILocationService,
            private $anchorScroll: any,
            private $timeout: ng.ITimeoutService,
            
            public govService: GovernanceCompanyProfileService,
            private NumberUtilityService: Common.NumberUtilityService
        ) {
            this.govProfile = this.$scope['governancectrl']; //TODO if there is time refactor to be a base controller
            this.currentCompanyId = this.govProfile.company.id;
            this.selectedIssue = this.govProfile.issueSelect;
            this.init();
        }

        private init(): void {
            var ctx = this;
            ctx.$rootScope['loading'] = true;
            ctx.govService.getKeyCgIssueTemplate().then(a => {
                ctx.templates = a;
                ctx.setupSelectedTemplate();
                ctx.showDataForSelectedTemplate(ctx.templateSelected);
                ctx.setupHash();
            },e => {
                console.log(e);
            }).finally(() => {
                ctx.$rootScope['loading'] = false;
            });
        }

        private setupHash() {
            var ctx = this;
            if (!ctx.selectedIssue) {
                ctx.govProfile.issueSelect = null;
                ctx.$location.hash("");
                 return;
            }
            ctx.$anchorScroll.yOffset = $("#topPanel").height();
            ctx.$location.hash(ctx.selectedIssue.number);
        }

        private goScrol() {
            var ctx = this;
            if (!ctx.selectedIssue) return;
            ctx.$anchorScroll.yOffset = $("#topPanel").height();
            ctx.$anchorScroll();
        }

        //getCompanyCgIssuesIndicators
        private setupSelectedTemplate(): void {
            var ctx = this;
            if (ctx.selectedIssue) {
                for (var i = 0; i < ctx.templates.length; i++) {
                    if (ctx.templates[i].number === ctx.selectedIssue.issueName) {
                        ctx.templateSelected = ctx.templates[i];
                        break;
                    }

                }
            } else {
                ctx.templateSelected = ctx.templates[0];
            }
        }

        private templateClick(templateClick: KeyCGIssueTemplateDto): void {
            if (this.selectedIssue && this.selectedIssue.issueName !== templateClick.number) {
                this.govProfile.issueSelect = null;
                this.$location.hash("");
            }
            this.templateSelected = templateClick;
            this.showDataForSelectedTemplate(this.templateSelected);
        }

        private showDataForSelectedTemplate(cgdto: Sustainalytics.Governance.KeyCGIssueTemplateDto):void {
            var ctx = this;
            console.log(ctx.templateSelected.name);
            if (!ctx.currentCompanyId) return;
            if (!cgdto) return;
            ctx.issueIndicatorsData = [];
            ctx.$rootScope['loading'] = true;

            var scoreAndAssement = ctx.govService.getCompanyCgIssuesIndicatorsScoreAndAssesment(ctx.currentCompanyId, cgdto.number);
            var overallData = ctx.govService.getCompanyCgIssuesIndicators(ctx.currentCompanyId, cgdto.number);

            ctx.$q.all([scoreAndAssement, overallData]).then(a => {
                if (a[0]) {
                    ctx.issueIndicatorsData = a[0];
                }
                if (a[1]) {
                    ctx.selectCgOverallIssues = a[1];
                    if (ctx.selectCgOverallIssues.issue) {
                        ctx.selectCgOverallIssues.issue.outlookAssesment = EsgUtility.getEsgOutlookTypeName(ctx.selectCgOverallIssues.issue.outlook);
                        ctx.selectCgOverallIssues.regionalPercentileOrdinal = ctx.NumberUtilityService.getOrdinalNameEnd(ctx.selectCgOverallIssues.percentile);
                        //console.log(ctx.selectCgOverallIssues.issue.comment);
                    }
                }
                ctx.$timeout(a => {
                    //ctx.$anchorScroll.yOffset = $("#topPanel").height();
                    //ctx.$anchorScroll();
                    ctx.goScrol();
                });

            }, e => {
                    console.log(e);
                }).finally(() => {
                ctx.$rootScope['loading'] = false;
            });
            
            //ctx.govService.getCompanyCgIssuesIndicatorsScoreAndAssesment(ctx.currentCompanyId, cgdto.number).then(a => {
            //    ctx.issueIndicatorsData = a;
            //    ctx.$rootScope['loading'] = false;
            //}, e => {
            //    ctx.$rootScope['loading'] = false;
            //    console.log(e);
            //});

        }

    
    }
    angular.module('Sustainalytics.Governance').controller('GovernanceIndicatorsDetailsController', ['$scope', '$rootScope', '$q', '$location', '$anchorScroll', '$timeout', 'GovernanceCompanyProfileService','NumberUtilityService', GovernanceIndicatorsDetailsController]);
} 